# Author:: Alexandre Ribeiro de Souza (<alexandre@lumis.com.br>)

tag('webserver_apache2')

include_recipe "apache2"
include_recipe "apache2::mod_alias"
include_recipe "apache2::mod_authz_host"
include_recipe "apache2::mod_deflate"
include_recipe "apache2::mod_dir"
include_recipe "apache2::mod_env"
include_recipe "apache2::mod_expires"
include_recipe "apache2::mod_headers"
include_recipe "lumis_apache2::mod_include"
include_recipe "apache2::mod_mime"
include_recipe "apache2::mod_negotiation"
include_recipe "apache2::mod_proxy"
include_recipe "apache2::mod_proxy_ajp"
include_recipe "apache2::mod_proxy_balancer"
include_recipe "apache2::mod_rewrite"
include_recipe "apache2::mod_setenvif"
include_recipe "apache2::mod_status"

client = data_bag_item("client", Chef::Config[:node_name].split('-')[1])

service "lumis_apache2" do
  case node[:platform]
  when "redhat","centos","scientific","fedora","suse","amazon"
    service_name "httpd"
  when "debian","ubuntu"
    service_name "apache2"
  end
  supports :reload => true, :status => true
  action :enable
end

template "#{node[:apache][:dir]}/mods-available/proxy_balancer.conf" do
  source "mods/proxy_balancer.conf.erb"
  notifies :reload, resources(:service => "lumis_apache2")
  variables(
    :javaservers => search(:node, "tags:javaserver* AND tags:client_#{Chef::Config[:node_name].split('-')[1]}")
  )
  mode 0644
end

apache_module "proxy_balancer"

web_app "lumisportal" do
  template "lumis_app.conf.erb"
  server_name client['url']
  server_aliases client['url_alias']
  docroot "#{node['lumis']['lumisportal']['home']}/htdocs"
end
